package bishi.shenxinfu;

import java.util.Arrays;

public class one {
    public static void main(String[] args) {
        one one = new one();
        System.out.println(Arrays.toString(one.nearestDiff(new int[]{1, 2, 3})));
    }
    public int[] nearestDiff(int[] nums) {
        // write code here
        int len = nums.length;
        int[] res = new int[len];
        for (int i = 0; i < len; i++) {
            int now = nums[i];
            int left = 0;
            int right = 0;
            int leftI = i - 1;
            int rightI = i + 1;
            while (leftI >= 0 && nums[leftI] >= now) leftI--;
            while (rightI < len && nums[rightI] <= now) rightI++;
            left = leftI == -1 ? 0 : nums[leftI];
            right = rightI == len ? 0 : nums[rightI];
            res[i] = right - left;
        }
        return res;
    }
}
